﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.CRM;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;

using Jzy.Model.CRM;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Web;


namespace Jzy.Web.CRM.SiteAccount
{
    public partial class List : PageList
    {
        private SiteAccountBO _siteAccount = new SiteAccountBO();


        private SiteAccountBO SiteAccount
        {
            get
            {
                if (_siteAccount == null)
                {
                    _siteAccount = new SiteAccountBO();
                }
                return _siteAccount;
            }
        }


        private PagerSortSetting _setting;

        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Footer1.InitPager(new BindViewHandler(BindView));

            if (!IsPostBack)
            {

                Pager.BindDrp(drpPager);
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            

            JSUtil.SetTableHeight("list", 145);

        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "网站账号");

            JSUtil.ShowForm(lnkAdd, "添加网站账号", null, GlobalVar.WebCurrPath + "Edit.aspx", 400, 253);
            //lnkAdd.HRef = "Edit.aspx";
           
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            

        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);


            _setting.SortDirection = Frame.Lib.Web.SortDirection.desc;
            _setting.SortExpression = "InputTime";

            Pager.SetPageSize(_setting, drpPager);

            string condition = "IsDelete=0 and (IsPublic = 1 or CreatorID=" + AppSession.UserID + ")";

            if (txtSearch.Text.Trim() != "")
            {
                condition += " and (Name like '%" + txtSearch.Text.Trim() + "%' or Url like '%" + txtSearch.Text.Trim() + "%')";
            }


            IEnumerable<SiteAccountInfo> list = this.SiteAccount.GetList(_setting, condition);


            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + _setting.RecordCount + "</b>&nbsp;个账号&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            //要删除相关的图片和附件
            this.SiteAccount.LogicDelete(strSelectIDs);
            BindView();
        }

       

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                JSUtil.ShowForm(lnkEdit, "修改网站账号", null, "Edit.aspx?ID=" + this.Encrypt(strID), 400, 253);

            }
        }
    }
}
